<template>
  <div class="device-nav" :style="{ height: containerHeight }">
    <ul class="nav-list">
      <li
        class="nav-item"
        v-for="(item, index) in navData"
        :key="index"
        :class="{ active: activeIndex === index }"
        @click="$emit('item-click', item, index)"
      >
        <div>
          <div class="nav-icon" :style="getIconStyle(item.id)"></div>
        </div>
        <div class="nav-text">
          {{ item.device_status }}
          <p v-if="item.subText" :style="getColorStyle(item.id)">
            {{ item.subText }}
          </p>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "DeviceNav",
  props: {
    // 导航数据列表
    navData: {
      type: Array,
      default: () => [],
      validator: (val) => val.every((item) => item.mainText), // 至少需要主文本
    },
    // 容器高度
    containerHeight: {
      type: String,
      default: "62%",
    },
    // 默认选中索引
    defaultActive: {
      type: Number,
      default: -1,
    },
    // 图标默认类型
    defaultIconType: {
      type: Number,
      default: 8,
    },
  },
  data() {
    return {
      activeIndex: this.defaultActive, // 当前选中索引
    };
  },
  watch: {
    defaultActive: {
      handler(newVal) {
        this.activeIndex = newVal;
      },
      immediate: true,
    },
  },
  methods: {
    // 外部可调用的选中方法
    setActive(index) {
      if (index >= 0 && index < this.navData.length) {
        this.activeIndex = index;
      }
    },
    getIconStyle(id) {
      if (id) {
        const bgMap = {
          'PL1': require("@/assets/img/Group1.png"),
          "PL2": require("@/assets/img/Group1.png"),
          "PL3": require("@/assets/img/Group1.png"),
          "WH1": require("@/assets/img/Group2.png"),
          "WM1": require("@/assets/img/Group2.png"),
          "WM2": require("@/assets/img/Group2.png"),
          "WM3": require("@/assets/img/Group2.png"),
          "TV1": require("@/assets/img/Group4.png"),
          "WH2": require("@/assets/img/Group3.png"),
          // 数据
          "BP1": require("@/assets/img/Group6.png"),
          "WM4": require("@/assets/img/Group3.png"),
          "WM5": require("@/assets/img/Group3.png"),
          "TV2": require("@/assets/img/Group4.png"),
          "OT1": require("@/assets/img/Group7.png"),
          "OT2": require("@/assets/img/Group8.png"),
          "OT3": require("@/assets/img/Group9.png"),
          "OT4": require("@/assets/img/Group10.png"),
          "OT2": require("@/assets/img/Group11.png"),
        };
        const imagePath = bgMap[id];
        if (imagePath) {
          return {
            backgroundImage: `url(${imagePath})`,
            backgroundSize: "cover",
            backgroundPosition: "center",
            backgroundRepeat: "no-repeat",
          };
        }
      }
    },
    getColorStyle(id) {
      // const bgMap = {
      //   1: "#45D3FF",
      //   2: "#FFB845",
      //   3: "#3DF3A1",
      // };
      if (id == 11 || id == 12 || id == 13 || id == 14) {
        return {
          color: `${"#FFB845"}`,
        };
      } else if (id == 15 || id == 16 || id == 17 || id == 18) {
        return {
          color: `${"#3DF3A1"}`,
        };
      } else {
        return {
          color: `${"#45D3FF"}`,
        };
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.device-nav {
  height: 841px;
  width: 255px;
  color: #fff;
  font-weight: 500;
  margin-top: 60px;
  // margin-bottom: 123px;
  overflow-y: auto; /* 垂直滚动 */
  // 滚动条样式
  &::-webkit-scrollbar {
    width: 13px;
    height: 813px;
  }
  &::-webkit-scrollbar-track {
    background: #a9acaf;
    border-radius: 10px; // 轨道圆角
    opacity: 0.7;
  }
  &::-webkit-scrollbar-thumb {
    background: #d9d9d9;
    border-radius: 4px;
    transition: background 0.2s;
  }
}
.nav-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 12px 10px;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;

  // 3D底纹效果
  // &::before {
  //   content: "";
  //   position: absolute;
  //   left: 10px;
  //   top: 8px;
  //   width: 32px;
  //   height: 32px;
  //   background: linear-gradient(45deg, #2c3e50, #34495e);
  //   border-radius: 8px;
  //   opacity: 0.8;
  //   z-index: 1;
  // }

  // 选中状态
  // &.active {
  //   background: rgba(255, 255, 255, 0.1);
  //   &::before {
  //     opacity: 1;
  //     box-shadow: 0 0 8px rgba(52, 152, 219, 0.3);
  //   }
  // }

  // 悬停效果
  &:hover {
    background: rgba(255, 255, 255, 0.05) !important;
  }
}

.nav-icon {
  width: 50px;
  height: 50px;
  margin-right: 10px;
  border-radius: 6px;
  flex-shrink: 0;
  z-index: 2; /* 覆盖底纹 */
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  font-size: 1px;
}

// 图标类型样式（可扩展）

.nav-text {
  font-size: 17px;
  white-space: nowrap;
  line-height: 1;
  z-index: 2;

  p {
    margin: 0.5rem 0 0 0; /* 优化间距 */
    color: rgb(13, 206, 239);
    font-size: 18px; /* 子文本稍小 */
  }
}
</style>